package com.spider.web.url;

import java.io.IOException;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;

public class HttpsClient {
	static final String LOGON_SITE = "member.ruten.com.tw";
	static final int LOGON_PORT = 443;
	// https://www.cn.alibaba-inc.com/staffinfo.nsf/Search?SearchView&Query=zhangg&SearchOrder=4

	public static void main(String[] args) {
		String SITE="https://www.cn.alibaba-inc.com";
		int PORT=80;
		Protocol myhttps = new Protocol("https", new MySecureProtocolSocketFactory (), 443);
		Protocol.registerProtocol("https ", myhttps);
		HttpClient client = new HttpClient();
		 
		client.getHostConfiguration().setHost( SITE, LOGON_PORT, myhttps);
	
	    System.setProperty("apache.commons.httpclient.cookiespec", "COMPATIBILITY");
	    //GetMethod authget = new GetMethod("/user/login.php");
	    GetMethod authget = new GetMethod("https://www.cn.alibaba-inc.com/staffinfo.nsf/Search?SearchView&Query=zhangg&SearchOrder=4");

	    try{
	      client.executeMethod(authget);
	    }
	    catch (Exception x)
	    {
	     
	    }
	    Header[] head = authget.getResponseHeaders();
	    Header[] foot = authget.getResponseFooters();
	    try{
	    	 System.out.println("Login form get: " +
	    			    authget.getStatusLine().toString() );
	    			    System.out.println(authget.getResponseBodyAsString());
		    }
		    catch (Exception x)
		    {
		     x.printStackTrace();
		    }
	   
	    authget.releaseConnection();

	    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();

	    Cookie[] initcookies =cookiespec.match(
	    		SITE,  PORT, "/", false,
	    client.getState().getCookies());

	    System.out.println("Initial set of cookies:");

	    if (initcookies.length == 0) {

	    System.out.println("None");

	    } else {

	    for (int i = 0; i < initcookies.length; i++) {
	    System.out.println("- " + initcookies[i].toString());
	    }

	    }

	    client.getState().addCookies(initcookies);
	    //PostMethod authpost = new PostMethod("/login.php?login_attempt=1");
	    PostMethod authpost = new PostMethod("https://www.cn.alibaba-inc.com/staffinfo.nsf/Search?SearchView&Query=zhangg&SearchOrder=4");
	    
	    // Prepare login parameters
/**
	    NameValuePair charset_test = new NameValuePair("charset_test", "&f");
	    NameValuePair version = new NameValuePair("version", "1.0");
	    NameValuePair return_session = new NameValuePair("return_session", "0");
	    NameValuePair pass = new NameValuePair("pass", "tiantian");
	    NameValuePair email = new NameValuePair("email", "rlby@tom.com");
	    NameValuePair persistent = new NameValuePair("persistent", "1");

	    authpost.setRequestBody(
	    new NameValuePair[] {charset_test, version, return_session, pass,email,persistent});
**/

	    try {
			client.executeMethod(authpost);
			System.out.print(authpost.getResponseBodyAsString());
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    


	    System.out.println("Login form post: " +
	    authpost.getStatusLine().toString());

	    authpost.releaseConnection();

	    Cookie[] logoncookies = cookiespec.match(
	     SITE,  PORT, "/", false,
	    client.getState().getCookies());

	    System.out.println("Logon cookies:");

	    if (logoncookies.length == 0) {

	    System.out.println("None");

	    } else {

	    for (int i = 0; i < logoncookies.length; i++) {
	    System.out.println("- " + logoncookies[i].toString());
	    }

	    }
	    int statuscode = authpost.getStatusCode();

	    if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
	    (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
	    (statuscode == HttpStatus.SC_SEE_OTHER) ||
	    (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {

	    Header header = authpost.getResponseHeader("location");

	    if (header != null) {

	    String newuri = header.getValue();

	    if ((newuri == null) || (newuri.equals(""))) {
	    newuri = "/";
	    }

	    System.out.println("Redirect target: " + newuri);
	    GetMethod redirect = new GetMethod(newuri);

	    try {
			client.executeMethod(redirect);
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    System.out.println("Redirect: " +
	    redirect.getStatusLine().toString());

	    redirect.releaseConnection();

	    } else {

	    System.out.println("Invalid redirect");
	    System.exit(1);

	    }
	    }
	    else
	    {
	    System.out.println("error");
	    System.exit(1);
	    }
	    }


		 


		
	}

 
